package com.google
{

	import flash.events.*;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.URLLoader;
	import mx.controls.Alert;
	import flash.net.URLRequestHeader;
	import mx.core.Application;
	
	public class Lists
	{
		private const link : String = "http://spreadsheets.google.com/feeds/spreadsheets/private/full";		
		
		private var _token : String;
		
		private var _received : String;


		//constructor
		public function Lists(tokenParam : String)
		{
			token = tokenParam;
		}

		//getters
		public function get token ():String
		{
			return _token;			
		}	

		public function get received ():String
		{
			return _received;			
		}			
		
		//setters
		public function set token (param:String):void
		{
			this._token = param;
		}	

		public function set received (param:String):void
		{
			this._received = param;
		}								
		
		//Functions		
		public function getListSpreadsheets():Boolean
		{
			
			
			var urlRequest : URLRequest  = new URLRequest(link);  
			urlRequest.method = URLRequestMethod.GET;	
			var header : String = "GoogleLogin auth="+token.substring(0,token.length);
			var rhArray:Array = new Array(new URLRequestHeader("Authorization",header));
			urlRequest.requestHeaders = rhArray;
            var ldr:URLLoader = new URLLoader ();
            ldr.load(urlRequest);
            ldr.addEventListener (Event.COMPLETE,onLoad); 
        	ldr.addEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        	ldr.addEventListener (HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        	ldr.addEventListener (IOErrorEvent.IO_ERROR, ioErrorHandler);            
            return true;  	
		}
		
		private function onLoad(event:Event):void
		{	
			received = event.target.data;	
			Application.application.textarea = received;
		}	

		private function securityErrorHandler (event : SecurityErrorEvent) : void
		{
		        Alert.show("securityErrorHandler: " + event);
		}
		private function httpStatusHandler (event : HTTPStatusEvent) : void
		{
		        trace("httpStatusHandler: " + event);
		}
		private function ioErrorHandler (event : IOErrorEvent) : void
		{
		        trace("ioErrorHandler: " + event);
		}		
	}
}